﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using eduscrum.DAL;
using eduscrum.Models;
using eduscrum.ViewModels;
using System;

namespace eduscrum.Controllers
{ 
    public class UserStoryController : Controller
    {
        private SystemContext db = new SystemContext();

        public ViewResult IndexProject(int projectID)
        {
            var userStories = db.UserStories.Where(u => u.Project.ProjectID == projectID).ToList();
            List<UserStoryVM> userStoriesVM = AutoMapper.Mapper.Map<List<UserStory>, List<UserStoryVM>>(userStories);
            return View(userStoriesVM.OrderBy(u => u.Priority));
        }

        public ViewResult IndexSprint(int sprintID)
        {
            var userStories = db.UserStories.Where(u => u.Project.ProjectID == sprintID).ToList();
            List<UserStoryVM> userStoriesVM = AutoMapper.Mapper.Map<List<UserStory>, List<UserStoryVM>>(userStories);
            ViewBag.SprintID = sprintID;
            return View(userStoriesVM);
        }

        //
        // GET: /UserStory/Create

        public ActionResult Create(int projectID)
        {
            Project project = db.Projects.Find(projectID);
            ProjectVM projectVM = new ProjectVM();
            AutoMapper.Mapper.Map(project, projectVM);
            var viewModel = new UserStoryVM { Project = projectVM };
            ViewData["Project"] = projectVM;
            return View("Create", viewModel);
        } 

        //
        // POST: /UserStory/Create

        [HttpPost]
        public ActionResult Create(UserStoryVM userStoryToCreate)
        {
            int projectID = -1;
            if (Request["ProjectID"] != null)
            {
                projectID = Int32.Parse(Request["ProjectID"].ToString());
                userStoryToCreate.Project = new ProjectVM { ProjectID = projectID };
            }
            if (ModelState.IsValid && projectID > 0)
            {
                Project projectToAddUserStoryTo = db.Projects.Find(projectID);
                UserStory newUserStory = new UserStory();
                AutoMapper.Mapper.Map(userStoryToCreate, newUserStory);
                if (projectToAddUserStoryTo.Phases.Count > 0)
                {
                    newUserStory.Phase = projectToAddUserStoryTo.Phases.First();
                }
                newUserStory.Project = projectToAddUserStoryTo;
                db.UserStories.Add(newUserStory);
                //db.Entry(projectToAddUserStoryTo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", Id = projectID }));
            }
            return View(userStoryToCreate);
        }
        
        //
        // GET: /UserStory/Edit/5

        public ActionResult Edit(int id)
        {
            UserStory userStoryToEdit = db.UserStories.Find(id);
            UserStoryVM viewModel = new UserStoryVM();
            AutoMapper.Mapper.Map(userStoryToEdit, viewModel);
            return View("Edit", viewModel);
        }

        //
        // POST: /UserStory/Edit/5

        [HttpPost]
        public ActionResult Edit(UserStoryVM userStoryToEditVM)
        {
            int projectID = Int32.Parse(Request["ProjectID"].ToString());
            userStoryToEditVM.Project = new ProjectVM { ProjectID = projectID };

            if (ModelState.IsValid)
            {
                UserStory userStoryEdited = db.UserStories.Find(userStoryToEditVM.UserStoryID);
                Project project = userStoryEdited.Project;
                AutoMapper.Mapper.Map(userStoryToEditVM, userStoryEdited);
                userStoryEdited.Project = project;
                db.Entry(userStoryEdited.Project).State = EntityState.Modified;
                db.Entry(userStoryEdited).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", Id = projectID }));
            }
            return View(userStoryToEditVM);
        }

        //
        // GET: /UserStory/Delete/5
 
        public ActionResult Delete(int id)
        {
            UserStory userstory = db.UserStories.Find(id);
            int projectID = userstory.Project.ProjectID;
            db.UserStories.Remove(userstory);
            db.SaveChanges();
            return RedirectToAction("Edit", new RouteValueDictionary(new { controller = "Project", action = "Edit", Id = projectID }));
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}